<?php
!function_exists('readover') && exit('Forbidden');
class PW_HouseSortElement {
	function PW_HouseSortElement() {
		$this->__construct();
	}
	function __construct() {
		global $db;
		$this->_db =& $db;
	}
	
	function newHouse($round='',$num=0,$type='area',$hid=0) {
		return $this->getHouses($round,'opendate',$num,$type,$hid);
	}
	function hotHouse($round='',$num=0,$type='area') {
		return $this->getHouses($round,'hits',$num,$type);
	}
	function groupBuyHouse($round='',$num=0,$type='area') {
		return $this->getHouses($round,'groupbuynum',$num,$type);
	}
	function collectHouse($round='',$num=0,$type='area') {
		return $this->getHouses($round,'collectnum',$num,$type);
	}
	function scoreHouse($round='',$num=0,$type='area') {
		return $this->getHouses($round,'score',$num,$type);
	}
	function recommendHouse($round='',$num=0,$type='area') {
		$num = (int) $num;
		!$num && $num = 10;
		$_sql_where = $this->getWhereSql($round,$type,1);
		$query = $this->_db->query("SELECT i.* FROM pw_house_info i LEFT JOIN pw_house_recommend r USING(hid) WHERE i.recommend=1 ORDER BY r.timestamp DESC ".pwLimit(0,$num));
		while ($rt = $this->_db->fetch_array($query)) {
			$temp[] = $this->_dataInit($rt);
		}
		return $temp;
	}
	
	function getHouseInfo($hid) {
		$temp = $this->_db->get_one('SELECT hid,name,logo,area,plate,status,aveprice,developer,opendate,isdimopendate,status,hits,groupbuynum,postnum,collectnum,score FROM pw_house_info WHERE hid='.pwEscape($hid));
		return $this->_dataInit($temp);
	}
	function getRoundHousesByPosition($hid,$num) {
		$houseMapDB = House::loadDB('housemap');
		$temp = $houseMapDB->searchHouseMapByPosition($hid,$num);
		foreach ($temp as $key=>$value) {
			$temp[$key] = $this->_dataInit($value);
		}
		return $temp;
	}
	
	function getHouses($round,$order,$num,$type='area',$hid=0) {
		$num = (int) $num;
		!$num && $num = 10;
		$_sql_where = $this->getWhereSql($round,$type,'',$hid);
		$_sql_order = $this->getOrderSql($order);
		$temp = array();
		$query = $this->_db->query("SELECT hid,name,logo,area,plate,status,aveprice,developer,opendate,isdimopendate,status,hits,groupbuynum,postnum,collectnum,score FROM pw_house_info $_sql_where $_sql_order ".pwLimit(0,$num));
		while ($rt = $this->_db->fetch_array($query)) {
			$temp[] = $this->_dataInit($rt);
		}
		return $temp;
	}
	
	function getOrderSql($order) {
		$order = $this->_initOrder($order);
		return ' ORDER BY '.$order.' DESC ';
	}
	
	function getWhereSql($round,$type='area',$add='',$hid=0) {
		switch ($type) {
			case 'area' :
				return $this->_areaRoundSql($round,$add);
			case 'price' :
				return $this->_priceRoundSql($round,$add,$hid);
		}
	}
	
	function _initOrder($order) {
		$allowOrder = array(
			'hid',
			'opendate',
			'hits',
			'groupbuynum',
			'postnum',
			'collectnum',
			'score'
		);
		if (in_array($order,$allowOrder)) {
			return $order;
		}
		return 'hid';
	}
	
	function _areaRoundSql($round,$add='') {
		$_sql_type = $add ? 'i.area' : 'area';
		return $round ? ' WHERE '.$_sql_type.'='.pwEscape($round):'';
	}
	
	function _priceRoundSql($round,$add='',$hid=0) {
		global $house_field;
		if(empty($house_field)){
			include_once (D_P . 'data/bbscache/house_config.php');
		}
		$round = (int) $round;
		$_sql_aveprice = $add ? 'i.aveprice' : 'aveprice';
		$_sql_hid = $add ? 'i.hid' : 'hid';
		if (!$round || !isset($house_field['aveprice'][$round])) return '';
		return " WHERE $_sql_aveprice>".pwEscape($house_field['aveprice'][$round]['start'])." AND $_sql_aveprice<".pwEscape($house_field['aveprice'][$round]['end'])." AND $_sql_hid<>".pwEscape($hid);
	}
	
	function _picRoundInit($round) {
		$round = (int) $round;
		if (!$round) return '';
		$imageTypeService = House::loadClass('houseimagetype'); /* @var $imageTypeService PW_HouseImageType */
		$imageType = $imageTypeService->getType($round);
		if ($imageType['sub']) return $round;
		$secendTypes = $imageTypeService->getSecendTypesById($round);
		$searchTypeid = array_keys($secendTypes);
		$searchTypeid[] = $round;
		
		return $searchTypeid;
	}
	
	function _dataInit($array) {
		global $houseBaseUrl;
		$data = array();
		$data['hid'] 	= $array['hid'];
		$data['url'] 	= $houseBaseUrl.'q=info&hid='.$array['hid'];
		$data['title'] 	= $array['name'];
		$data['name'] 	= $array['name'];
		$data['hits'] 	= $array['hits'];
		$data['aveprice'] 	= $array['aveprice'];
		$data['value'] 	= $array['aveprice'];
		$data['area'] 	= $this->_getArea($array['area']);
		$data['image']	= $this->_getImageUrl($array['logo']);
		$array['opendate'] && $data['opendate'] = !$array['isdimopendate'] ? get_date($array['opendate'],'Y-n-j') : $this->_getDimDate($array['opendate']);
		$data['state'] = $this->_getStatus($array['status']);
		return $data;
	}
	
	function _getDimDate($date){
		if(date('d', $date) < 10) $d = '上旬';
		if(date('d', $date) >= 10 && date('d', $date) <= 20) $d = '中旬';
		if(date('d', $date) > 20) $d = '下旬';
		return date('n').'月'.$d;
	}
	
	function _getStatus($type) {
		switch ($type) {
			case 1 :
				return '新盘';
			case 2 :
				return '在售';
			case 3 :
				return '尾盘';
			case 4 :
				return '售空';
			default :
				return '新盘';
		}
	}
	
	function _getArea($area) {
		global $house_field;
		if(empty($house_field)){
			include_once (D_P . 'data/bbscache/house_config.php');
		}
		if (isset($house_field['area'][$area])) return $house_field['area'][$area]['name'];
	}
	
	function _getImageUrl($imageUrl) {
		$temp = geturl($imageUrl);
		if (!$temp) return false;
		return $temp[0];
	}
}
?>